const loadView = require('./_import_' + process.env.NODE_ENV)

import AppMain from "@/views/main/index.vue";

import {
	getRouter
} from '../api/router.js'

export const getRouters = () => {
	return new Promise((resolve) => {
		getRouter().then(res => {
			var list = filterAsyncRouter(res)
			resolve(list)
		})
	})


}


function filterAsyncRouter(routers) {
	// 遍历后台传来的路由字符串，转换为组件对象
	let accessedRouters = routers.filter(router => {
		if (router.meta) {
			// 默认图标处理
			router.meta.icon = router.meta.icon ? router.meta.icon : "component";
		}
		if (router.component === "Layout") {
			// Main组件特殊处理
			router.component = AppMain;
		} else {
			//处理组件---重点
			router.component = loadView(router.component);
		}
		//存在子集
		if (router.children && router.children.length) {
			router.children = filterAsyncRouter(router.children);
		}
		return true;
	});
	return accessedRouters;
}
